/**
 * 
 */
package com.eclipse.org.snms.reminder;

import com.eclipse.org.snms.*;

//TODO: fix import to only import interfaces necessary
import com.eclipse.org.snms.serializer.loadStickyNotes.*; // import serlializer subject and observer interfaces
import com.eclipse.org.snms.summary.*; // import summary subject and observer interfaces
import java.util.ArrayList;
import java.util.Calendar;	// calendar class to obtain current time

/**
 * @author dimitri.tiago
 */
public class ReminderSubjectConcrete implements ReminderSubject, SummaryObserver, SerializerObserver
{
	
	// instance variables
	private static ArrayList<ReminderObserver> subscriberList ;
	private static ArrayList<StickyNote> stickyNoteList;
	
	// default constructor
	public ReminderSubjectConcrete()
	{
		// no-op
		
		// initialise instance variables
		subscriberList = new ArrayList<ReminderObserver>();
		stickyNoteList = new ArrayList<StickyNote>();
	}
	
	// constructor - registers ReminderSubjectConcrete with Summary and Serializer plug-ins
	public ReminderSubjectConcrete(SummarySubject summary, SerializerSubject serializer)
	{
		// initialise instance variables--- newly added by dimi
		subscriberList = new ArrayList<ReminderObserver>();
		stickyNoteList = new ArrayList<StickyNote>();
		
		// subscribe as an observer to summary subject
		summary.subscribe(this);
		
		// subscribe as an observer to serializer
		serializer.subscribe(this);
	}

	// add observer to subscriber list
	public void subscribe(ReminderObserver observer)
	{
		subscriberList.add(observer);
	}

	// remove observer from subscriber list
	public void unsubscribe(ReminderObserver observer)
	{
		subscriberList.remove(observer);
	}

	// notify observers in subscriber list
	public void notify(StickyNote stickyNote)
	{
		for (ReminderObserver observer : subscriberList)
		{	
			observer.update(stickyNote);	
		}
	}

	// update sticky note list
	public void update(StickyNote stickyNote, int transType)
	{
		// add sticky note to list
		if ( transType == 0 )
		{
			this.stickyNoteList.add(stickyNote);
		}
		
		// update sticky note in list
		if ( transType == 1 )
		{
			// remove sticky note from list and add updated sticky note
			this.stickyNoteList.remove(stickyNote);
			this.stickyNoteList.add(stickyNote);
		}
		
		// remove sticky note from list
		if ( transType == 2 )
		{
			this.stickyNoteList.remove(stickyNote);
		}		
	}

	// update sticky note list
	public void update(ArrayList<StickyNote> stickyNoteList)
	{
		for (StickyNote stickyNote : stickyNoteList)
		{
			this.stickyNoteList.add(stickyNote);
		}
	}
	
	// check sticky note deadline
	public void checkDeadlines()
	{	
		for (StickyNote stickyNote : stickyNoteList)
		{	
			// if sticky note is active, check deadline
			if ( ( (TaskComposite) stickyNote ).getIsActive() == 1)
			{
				// get current date and time
				Calendar currentCalendar = Calendar.getInstance();
					
				// get sticky note deadline
				Calendar stickyNoteDeadlineCalendar = ( (TaskComposite) stickyNote ).getDeadline();
				
				// check deadline
				if ( stickyNoteDeadlineCalendar.before(currentCalendar) )
				{
					// raise sticky note alert
					alert(stickyNote);
				}
			}
		}
	}
	
	// disable sticky note alarm (i.e. confirm)
	private void disableReminder(StickyNote stickyNote)
	{
		( (TaskComposite) stickyNote ).setIsActive(0);
		
		// notify observers of alarm disabled (i.e. sticky note expired)
		notify(stickyNote);
	}
	
	// TODO
	private void alert(StickyNote stickyNote)
	{
		// TODO
		
		String showAlert = "Stick Note Project " + stickyNote.getId() + " \n" +
		((TaskComposite)stickyNote).getTitle() + " gets expired now. \nIts description: " +
		stickyNote.getDescription();
		ReminderGUI concreteReminder = new ReminderGUI(showAlert);
		
			while(true) {
			     ;
			
				if (concreteReminder.getClicked()) {
					break;
				}
			}
	
			
			if (concreteReminder.getConfirm() == true) {
				disableReminder(stickyNote);
				System.out.println("in the alert method, confirm the stickynote \n");
				printOutlist();
				System.out.println("\n\n\n");
			} else {
				Calendar currentCalendar1 = Calendar.getInstance();
				currentCalendar1.add(Calendar.MINUTE, +10);
				((TaskComposite)stickyNote).setDeadline(currentCalendar1);
				System.out.println("in the alert method, snnoze the stickynote \n");
				printOutlist();
				System.out.println("\n\n\n");
			}
			
	}
	
	//Testing: printOutlist is for printing out all the stickynotes in the reminder list
	private void printOutlist() {
		String message = null;
		for (int i =0; i<stickyNoteList.size(); i++) {
			TaskComposite task = (TaskComposite)stickyNoteList.get(i);
			message = "StickyNote: Title - " + task.getTitle() + "\n" +
						"Deadline - " + task.getDeadline().toString() + "\n" +
						"Priority - " + task.getPriority() + "\n" +
						"Active value - " + task.getIsActive() + "\n" +
						"Description - " + task.getDescription()+ "\n" +
						"ID - " + task.getId()+ "\n";
			System.out.print(message);
		}
	}	
}